<?php


namespace app\home\controller;

use app\home\model\Huanzhe as HuanzheModel;
use app\home\model\Admin as AdminModel;
use think\Controller;
use think\Request;

class Patient extends Controller
{

    protected $huanzheModel;
    protected $adminModel;

    public function __construct(
        HuanzheModel $huanzheModel,
        AdminModel $adminModel
    )
    {
        parent::__construct();
        $this->huanzheModel = $huanzheModel;
        $this->adminModel = $adminModel;
    }

    public function show(Request $request)
    {
        $id = $request->param('id');
        $info = [];
        if($id){
            $info = $this->huanzheModel->find($id);
        }
        $this->assign('info', $info);
        $list = getAllOption();
        $user_id = session('user_id');
        $user = $this->adminModel->with('hospital')->find($user_id);
        $hospital = $user['hospital'];
        $keshi = getOptionByName($list, '科室');
        $gerenbingshi = getOptionByName($list, '个人病史');
        $xiyan = getOptionByName($list, '吸烟');
        $yinjiu = getOptionByName($list, '饮酒');
        $hecha = getOptionByName($list, '喝茶');
        $shenghuodi = getOptionByName($list, '生活地');
        $jiaoyuchengdu = getOptionByName($list, '教育程度');
        $muqianzhiye = getOptionByName($list, '目前职业');
        $hunyinzhuangtai = getOptionByName($list, '婚姻状态');
        $minzu = getOptionByName($list, '民族');
        $this->assign('hospital', $hospital);
        $this->assign('keshi', $keshi);
        $this->assign('gerenbingshi', $gerenbingshi);
        $this->assign('xiyan', $xiyan);
        $this->assign('yinjiu', $yinjiu);
        $this->assign('hecha', $hecha);
        $this->assign('shenghuodi', $shenghuodi);
        $this->assign('jiaoyuchengdu', $jiaoyuchengdu);
        $this->assign('muqianzhiye', $muqianzhiye);
        $this->assign('hunyinzhuangtai', $hunyinzhuangtai);
        $this->assign('minzu', $minzu);
        return $this->fetch();
    }

    public function lists(){
        return $this->fetch();
    }

    public function ajax_lists(Request $request){
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        $where = [];
        $data = $this->huanzheModel->where($where)->page($page,$limit)->order('id','desc')->select();
        $count = $this->huanzheModel->where($where)->count();
        return reorganizeTableData(0,'',$count,$data);
    }

    public function info(Request $request)
    {
        $id = $request->param('pid', '');
        $res = $this->huanzheModel->with('renticeliang')->find($id);
        $res['age'] = '';
        if(isset($res['birthday']) && !empty($res['birthday'])){
            $res['age'] = countAge($res['birthday']);
        }
        return jsonRes($res);
    }

    public function add(Request $request)
    {
        $yyid                   = $request->param('yyid', null);
        $yyname                 = $request->param('yyname', null);
        $name                   = $request->param('name', null);
        $gender                 = $request->param('gender', '');
        $birthday               = $request->param('birthday', null);
        $rydate                 = $request->param('rydate', null);
        $is_juejing             = $request->param('is_juejing', 2);
        $juejing                = $request->param('juejing', '');
        $hospital_id            = $request->param('hospital_id', null);
        $keshi                  = $request->param('keshi', null);
        $ksid                   = $request->param('ksid', null);
        $bingshi                = $request->param('bingshi', null);
        $is_zljzs               = $request->param('is_zljzs', '');
        $xiyan                  = $request->param('xiyan', '');
        $xiyan_nums             = $request->param('xiyan_nums', null);
        $xiyan_year             = $request->param('xiyan_year', null);
        $xiyan_type             = $request->param('xiyan_type', '');
        $xiyan_type_id          = $request->param('xiyan_type_id', 0);
        $yinjiu                 = $request->param('yinjiu', '');
        $yinjiu_nums            = $request->param('yinjiu_nums', null);
        $jiulist                = $request->param('jiulist', null);
        $pijiu_nums             = $request->param('pijiu_nums', null);
        $putao_huangjiu_nums    = $request->param('putao_huangjiu_nums', null);
        $baijiu_nums            = $request->param('baijiu_nums', null);
        $cha                    = $request->param('cha', '');
        $cha_nums               = $request->param('cha_nums', null);
        $cha_list               = $request->param('cha_list', null);
        $area_id                = $request->param('area_id', null);
        $area                   = $request->param('area', '');
        $my_mobile              = $request->param('my_mobile', null);
        $wife_mobile            = $request->param('wife_mobile', null);
        $tel                    = $request->param('tel', null);
        $son_mobile             = $request->param('son_mobile', null);
        $son_tel                = $request->param('son_tel', null);
        $nver_mobile            = $request->param('nver_mobile', null);
        $nver_tel               = $request->param('nver_tel', null);
        $address                = $request->param('address', null);
        $zipcode                = $request->param('zipcode', null);
        $jiaoyu                 = $request->param('jiaoyu', '');
        $jiaoyu_id              = $request->param('jiaoyu_id', null);
        $zhiye                  = $request->param('zhiye', '');
        $zhiye_id               = $request->param('zhiye_id', 0);
        $hunyin                 = $request->param('hunyin', '');
        $hunyin_id              = $request->param('hunyin_id', 0);
        $minzu_id              = $request->param('minzu_id', 0);
        $minzu                  = $request->param('minzu', '');
        //$kps_id                 = $request->param('kps_id', null);

        $bianhao = randStr(10);

        $data = [
            'bianhao'               => $bianhao,
            'yyid'                  => $yyid,
            'yyname'                => $yyname,
            'name'                  => $name,
            'gender'                => $gender,
            'birthday'              => $birthday,
            'rydate'                => $rydate,
            'is_juejing'            => $is_juejing,
            'juejing'               => $juejing,
            'hospital_id'           => $hospital_id,
            'keshi'                 => $keshi,
            'ksid'                  => $ksid,
            'bingshi'               => $bingshi,
            'is_zljzs'              => $is_zljzs,
            'xiyan'                 => $xiyan,
            'xiyan_nums'            => $xiyan_nums,
            'xiyan_year'            => $xiyan_year,
            'xiyan_type'            => $xiyan_type,
            'xiyan_type_id'         => $xiyan_type_id,
            'yinjiu'                => $yinjiu,
            'yinjiu_nums'           => $yinjiu_nums,
            'jiulist'               => $jiulist,
            'pijiu_nums'            => $pijiu_nums,
            'putao_huangjiu_nums'   => $putao_huangjiu_nums,
            'baijiu_nums'           => $baijiu_nums,
            'cha'                   => $cha,
            'cha_nums'              => $cha_nums,
            'cha_list'              => $cha_list,
            'area_id'               => $area_id,
            'area'                  => $area,
            'my_mobile'             => $my_mobile,
            'wife_mobile'           => $wife_mobile,
            'tel'                   => $tel,
            'son_mobile'            => $son_mobile,
            'son_tel'               => $son_tel,
            'nver_mobile'           => $nver_mobile,
            'nver_tel'              => $nver_tel,
            'address'               => $address,
            'zipcode'               => $zipcode,
            'jiaoyu'                => $jiaoyu,
            'jiaoyu_id'             => $jiaoyu_id,
            'zhiye'                 => $zhiye,
            'zhiye_id'              => $zhiye_id,
            'hunyin'                => $hunyin,
            'hunyin_id'             => $hunyin_id,
            'minzu_id'              => $minzu_id,
            'minzu'                 => $minzu
        ];
        $res = $this->huanzheModel->save($data);
        if($res){
            $resBack = jsonRes($res);
        }else{
            $resBack = jsonRes($res, '新增失败', 99);
        }
        return $resBack;
    }

    public function update(Request $request)
    {
        $id                     = $request->param('id');
        if(!isset($id) || empty($id)){
            return jsonRes([],'id不能为空',99);
        }
        $yyid                   = $request->param('yyid', null);
        $yyname                 = $request->param('yyname', null);
        $name                   = $request->param('name', null);
        $gender                 = $request->param('gender', '');
        $birthday               = $request->param('birthday', null);
        $rydate                 = $request->param('rydate', null);
        $is_juejing             = $request->param('is_juejing', 2);
        $juejing                = $request->param('juejing', '');
        $hospital_id            = $request->param('hospital_id', null);
        $keshi                  = $request->param('keshi', null);
        $ksid                   = $request->param('ksid', null);
        $bingshi                = $request->param('bingshi', null);
        $is_zljzs               = $request->param('is_zljzs', '');
        $xiyan                  = $request->param('xiyan', '');
        $xiyan_nums             = $request->param('xiyan_nums', null);
        $xiyan_year             = $request->param('xiyan_year', null);
        $xiyan_type             = $request->param('xiyan_type', '');
        $xiyan_type_id          = $request->param('xiyan_type_id', 0);
        $yinjiu                 = $request->param('yinjiu', '');
        $yinjiu_nums            = $request->param('yinjiu_nums', null);
        $jiulist                = $request->param('jiulist', null);
        $pijiu_nums             = $request->param('pijiu_nums', null);
        $putao_huangjiu_nums    = $request->param('putao_huangjiu_nums', null);
        $baijiu_nums            = $request->param('baijiu_nums', null);
        $cha                    = $request->param('cha', '');
        $cha_nums               = $request->param('cha_nums', null);
        $cha_list               = $request->param('cha_list', null);
        $area_id                = $request->param('area_id', null);
        $area                   = $request->param('area', '');
        $my_mobile              = $request->param('my_mobile', null);
        $wife_mobile            = $request->param('wife_mobile', null);
        $tel                    = $request->param('tel', null);
        $son_mobile             = $request->param('son_mobile', null);
        $son_tel                = $request->param('son_tel', null);
        $nver_mobile            = $request->param('nver_mobile', null);
        $nver_tel               = $request->param('nver_tel', null);
        $address                = $request->param('address', null);
        $zipcode                = $request->param('zipcode', null);
        $jiaoyu                 = $request->param('jiaoyu', '');
        $jiaoyu_id              = $request->param('jiaoyu_id', null);
        $zhiye                  = $request->param('zhiye', '');
        $zhiye_id               = $request->param('zhiye_id', 0);
        $hunyin                 = $request->param('hunyin', '');
        $hunyin_id              = $request->param('hunyin_id', 0);
        $minzu_id              = $request->param('minzu_id', 0);
        $minzu                  = $request->param('minzu', '');
        //$kps_id                 = $request->param('kps_id', null);

        $data = [
            'yyid'                  => $yyid,
            'yyname'                => $yyname,
            'name'                  => $name,
            'gender'                => $gender,
            'birthday'              => $birthday,
            'rydate'                => $rydate,
            'is_juejing'            => $is_juejing,
            'juejing'               => $juejing,
            'hospital_id'           => $hospital_id,
            'keshi'                 => $keshi,
            'ksid'                  => $ksid,
            'bingshi'               => $bingshi,
            'is_zljzs'              => $is_zljzs,
            'xiyan'                 => $xiyan,
            'xiyan_nums'            => $xiyan_nums,
            'xiyan_year'            => $xiyan_year,
            'xiyan_type'            => $xiyan_type,
            'xiyan_type_id'         => $xiyan_type_id,
            'yinjiu'                => $yinjiu,
            'yinjiu_nums'           => $yinjiu_nums,
            'jiulist'               => $jiulist,
            'pijiu_nums'            => $pijiu_nums,
            'putao_huangjiu_nums'   => $putao_huangjiu_nums,
            'baijiu_nums'           => $baijiu_nums,
            'cha'                   => $cha,
            'cha_nums'              => $cha_nums,
            'cha_list'              => $cha_list,
            'area_id'               => $area_id,
            'area'                  => $area,
            'my_mobile'             => $my_mobile,
            'wife_mobile'           => $wife_mobile,
            'tel'                   => $tel,
            'son_mobile'            => $son_mobile,
            'son_tel'               => $son_tel,
            'nver_mobile'           => $nver_mobile,
            'nver_tel'              => $nver_tel,
            'address'               => $address,
            'zipcode'               => $zipcode,
            'jiaoyu'                => $jiaoyu,
            'jiaoyu_id'             => $jiaoyu_id,
            'zhiye'                 => $zhiye,
            'zhiye_id'              => $zhiye_id,
            'hunyin'                => $hunyin,
            'hunyin_id'             => $hunyin_id,
            'minzu_id'              => $minzu_id,
            'minzu'                 => $minzu
        ];
        $res = $this->huanzheModel->save($data,['id' => $id]);
        if($res){
            $resBack = jsonRes($res);
        }else{
            $resBack = jsonRes($res, '修改失败', 99);
        }
        return $resBack;
    }

    public function kspf_show(){
        return $this->fetch('kspf/show');
    }

    public function kspf_update(Request $request){
        $pid = $request->param('pid', '');
        if(!isset($pid) || empty($pid)){
            return jsonRes([],'pid不能为空',99);
        }
        $data = [
            'kps_id' => $request->param('kps_id', 0)
        ];
        $res = $this->huanzheModel->save($data,['id' => $pid]);
        if($res){
            $resBack = jsonRes($res);
        }else{
            $resBack = jsonRes($res, '修改失败', 99);
        }
        return $resBack;
    }

    public function shzl_info(Request $request){
        $pid = $request->param('pid', '');
        $res = $this->huanzheModel->where('id', $pid)->find();
        $info = null;
        if(!empty($res['shzl_info'])){
            $info = json_decode($res['shzl_info'],1);
        }
        return jsonRes($info);

    }
    public function shzl_show(){
        return $this->fetch('shzl/show');
    }

    public function shzl_update(Request $request){
        $pid = $request->param('pid', '');
        if(!isset($pid) || empty($pid)){
            return jsonRes([],'pid不能为空',99);
        }
        $data = [
            'shzl_info' => json_encode($request->param('shzl_info'),1)
        ];
        //return json($data);
        $res = $this->huanzheModel->save($data,['id' => $pid]);
        if($res){
            $resBack = jsonRes($res);
        }else{
            $resBack = jsonRes($res, '修改失败', 99);
        }
        return $resBack;
    }

    public function lcjj_info(Request $request){
        $pid = $request->param('pid', '');
        $res = $this->huanzheModel->where('id', $pid)->find();
        $info = null;
        if(!empty($res['lcjj_info'])){
            $info = json_decode($res['lcjj_info'],1);
        }
        return jsonRes($info);

    }
    public function lcjj_show(){
        return $this->fetch('lcjj/show');
    }

    public function lcjj_update(Request $request){
        $pid = $request->param('pid', '');
        if(!isset($pid) || empty($pid)){
            return jsonRes([],'pid不能为空',99);
        }
        $data = [
            'lcjj_info' => json_encode($request->param('lcjj_info'),1)
        ];
        $res = $this->huanzheModel->save($data,['id' => $pid]);
        if($res){
            $resBack = jsonRes($res);
        }else{
            $resBack = jsonRes($res, '修改失败', 99);
        }
        return $resBack;
    }
}